<?php

namespace app\common\model;

use think\Db;
use think\Model;


class AgentShip extends Model
{

    

    

    // 表名
    protected $name = 'agent_ship';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
			'type_text'
    ];
	
    const TYPE_SELF = 1;
    const TYPE_SON = 2;
    
    public static $typeList = [
    	self::TYPE_SELF => '自己核销',
	    self::TYPE_SON => '下级核销'
    ];
    
    public function getTypeTextAttr($value, $data) {
    	$value = $value ? : $data['type'];
    	$list = self::$typeList;
    	return $list[$value] ?? '';
    }
	
	
	
	public function agent() {
		return $this->belongsTo('agentShop', 'agent_shop_id', 'id', '', 'left')->setEagerlyType(0);
	}
	public function goods() {
		return $this->belongsTo('goods', 'goods_id', 'id', '', 'left')->setEagerlyType(0);
	}
	public function spec() {
		return $this->belongsTo('goodsSpec', 'spec_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	/**
	 * 添加核销记录
	 * @param $orderId 订单ID
	 * @param $agentId 代理ID
	 * @param $goodsId 商品ID
	 * @param $specId 规格ID
	 * @param $number 数量
	 * @param $code 核销码
	 * @return int|string
	 */
	public function addRecore($orderId, $agentId, $goodsId, $specId, $number, $code, $type = self::TYPE_SELF) {
		return Db::name('agentShip')
			->insert([
				'order_id' => $orderId,
				'agent_shop_id' => $agentId,
				'goods_id' => $goodsId,
				'spec_id' => $specId,
				'number' => $number,
				'code' => $code,
				'create_time' => time(),
				'update_time' => time(),
				'type' => $type
			]);
	}




}
